package com.gw.yunke.service.impl;

import com.gw.yunke.constant.Constant;
import com.gw.yunke.dao.QAndAMapper;
import com.gw.yunke.dao.UserQuestionMapper;
import com.gw.yunke.domain.QAndA;
import com.gw.yunke.domain.QAndAExample;
import com.gw.yunke.domain.UserQuestion;
import com.gw.yunke.domain.UserQuestionExample;
import com.gw.yunke.domain.dto.QAndAReq;
import com.gw.yunke.domain.dto.UserAnswerReq;
import com.gw.yunke.domain.dto.UserQAndAInfoResp;
import com.gw.yunke.service.IQAndAService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QAndAServiceImpl implements IQAndAService{

  private static final Logger log = LoggerFactory.getLogger(QAndAServiceImpl.class);
  @Autowired
  private QAndAMapper qAndAMapper;
  @Autowired
  private UserQuestionMapper userQuestionMapper;

  @Override
  public List<QAndA> getQuestionSingleSelectByCourseId(Long courseId) {
    QAndAExample example = new QAndAExample();
    QAndAExample.Criteria criteria = example.createCriteria();
    criteria.andCourseIdEqualTo(courseId);
    criteria.andTypeEqualTo(Constant.QAQ_SINGLESELECT);
    return qAndAMapper.selectByExample(example);
  }

  @Override
  public int createQuestion(QAndAReq qAndAReq) {
    QAndA qAndA = new QAndA();
    try {
      qAndA.setQuestion(qAndAReq.getQuestion());
      qAndA.setAnswer(qAndAReq.getAnswer());
      qAndA.setType(qAndAReq.getType());
      qAndA.setCourseId(qAndAReq.getCourseId());
      qAndA.setQuestionOrder(qAndAReq.getOrder());
      int result = qAndAMapper.insertSelective(qAndA);
      return result;
    }catch (Exception e){
      log.error("添加题目失败!courseId = {}",qAndAReq.getCourseId(),e);
      return 0;
    }
  }

  @Override
  public int createAnswer(UserAnswerReq userAnswerReq) {
    UserQuestion userQuestion = new UserQuestion();
    try {
      userQuestion.setQuestionId(userAnswerReq.getQuestionId());
      userQuestion.setUserId(userAnswerReq.getUserId());
      userQuestion.setUserAnswer(userAnswerReq.getAnswer());
      int result = userQuestionMapper.insertSelective(userQuestion);
      return result;
    }catch (Exception e){
      log.error("添加回答失败!courseId = {}",userAnswerReq.getCourseId(),e);
      return 0;
    }
  }

  @Override
  public List<UserQAndAInfoResp> getUserAnswerInfoByCourseId(Long courseId,Long userId) {
    UserQuestionExample example = new UserQuestionExample();
    UserQuestionExample.Criteria criteria = example.createCriteria();
    criteria.andQuestionIdIn(getQuestionIdByCourseId(courseId));
    criteria.andUserIdEqualTo(userId);
    List<UserQuestion> userQuestionList = userQuestionMapper.selectByExample(example);
    List<UserQAndAInfoResp> userQAndAInfoRespList = new ArrayList<>();
    for(UserQuestion userQuestion : userQuestionList){
      UserQAndAInfoResp userQAndAInfoResp = new UserQAndAInfoResp();
      userQAndAInfoResp.setQuestionId(userQuestion.getQuestionId());
      QAndA qAndA = qAndAMapper.selectByPrimaryKey(userQuestion.getQuestionId());
      userQAndAInfoResp.setQuestionOrder(qAndA.getQuestionOrder());
      userQAndAInfoResp.setQuestion(qAndA.getQuestion());
      userQAndAInfoResp.setRightAnswer(qAndA.getAnswer());
      userQAndAInfoResp.setUserAnswer(userQuestion.getUserAnswer());
      userQAndAInfoResp.setIsRight(qAndA.getAnswer().equals(userQuestion.getUserAnswer()) ? (byte)1 : (byte)0);
      userQAndAInfoRespList.add(userQAndAInfoResp);
    }
    return userQAndAInfoRespList;
  }

  private List<Long> getQuestionIdByCourseId(Long courseId){
    QAndAExample example = new QAndAExample();
    QAndAExample.Criteria criteria = example.createCriteria();
    criteria.andCourseIdEqualTo(courseId);
    List<Long> questionIds = new ArrayList<>();
    List<QAndA> qAndAList = qAndAMapper.selectByExample(example);
    for(QAndA qAndA : qAndAList){
      questionIds.add(qAndA.getId());
    }
    return questionIds;
  }
}
